package controller;

import entity.hotel_employee;
import service.HotelEmployeeService;
import serviceImp.HotelEmployeeServiceImp;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;



/**
 * Servlet implementation class LoginController
 */
@WebServlet("/loginRegister/LoginController.do")
public class LoginController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String tel = request.getParameter("tel");
		String password = request.getParameter("password");

		HotelEmployeeService hotelEmployeeService =new HotelEmployeeServiceImp();
		int code=hotelEmployeeService.login(tel,password);
		if(code>0){
			HttpSession session = request.getSession();
			String employeeName = hotelEmployeeService.selectNameByTel(tel);
			hotel_employee hotel_employee = hotelEmployeeService.selectInfoByTel(tel);
			String employeeJob = hotel_employee.getEmployee_job();
			session.setAttribute("loginTel",tel);
			session.setAttribute("employeeName",employeeName);
			session.setAttribute("employeeJob",employeeJob);

			response.sendRedirect("/hotelSystem/main/main.jsp");
		}else{
			switch (code){
				case -1:
					request.setAttribute("loginTip","登录失败，账号不存在");
					break;
				case 0:
					request.setAttribute("loginTip", "登录失败，密码错误");
					break;
			}
			request.getRequestDispatcher("/loginRegister/loginRegister.jsp").forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
